<?php
/* 
 * Copyright (c) 2011 Antoine d'Otreppe de Bouvette
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

// Set up the debug mode
if (!defined('DEBUG')) {
    define('DEBUG', false);
}

if ('DEBUG') {
    ini_set("display_errors", 1);
    error_reporting(E_ALL | E_NOTICE);
}

define('ASPYCT', __DIR__);
define('ASPYCT_CLASS_DIR', __DIR__.'/classes');

final class Aspyct {
    const VERSION = '0.1';
    const RELEASE_DATE = '11.10.17';
    
    const CLASS_FILE_PATTERN = '#(?P<class>[A-Z][\w\d]*)\.php#';
    
    private static $classList;
    
    public static function loadClass($class) {
        $filename = sprintf("%s/%s.php", ASPYCT_CLASS_DIR, $class);

        if (is_file($filename)) {
            require_once($filename);
        }
    }
    
    public static function listClasses() {
        if (self::$classList === null) {
            self::$classList = array();

            $dir = opendir(ASPYCT_CLASS_DIR);
            while(($file = readdir($dir)) !== false) {
                if (preg_match(self::CLASS_FILE_PATTERN, $file, $match)) {
                    $name = $match['class'];
                    self::$classList[$name] = self::getClass($name);
                }
            }
            closedir($dir);
        }
        
        return self::$classList;
    }
    
    /**
     * @param string $name The name of the class
     * @return ReflectionDocumentedClass
     */
    public static function getClass($name) {
        return new ReflectionDocumentedClass($name);
    }
    
    /**
     * @param string $name 
     * @return boolean true if this is an Aspyct class
     */
    public static function classExists($name) {
        return array_key_exists($name, self::listClasses());
    }
}

spl_autoload_register('Aspyct::loadClass');

function array_get(&$array, $key, $default=null) {
    if (array_key_exists($key, $array)) {
        return $array[$key];
    }
    else {
        return $default;
    }
}

function setupGettext($directory, $locale) {
    throw new RuntimeException("Not yet implemented");
}

function _e($text) {
    echo escape(_($text));
}

function _x($text /*, $varargs */) {
    echo call_user_func_array('_s', func_get_args());
}

function _s($text /*, $varargs */) {
    $args = func_get_args();
    array_shift($args);
    
    return escape(vsprintf(_($text), $args));
}

function put($text) {
    echo escape($text);
}

function escape($text) {
    return htmlentities($text, ENT_QUOTES);
}

function debug($arg) {
    highlight_string(var_export($arg, true));
}

function _clone($object) {
    return $object !== null ? clone $object : null;
}

function inheritIni($filename) {
    $realpath = realpath($filename);
    
    if ($realpath !== false) {
        $dir = dirname($realpath) . '/';
        $values = array('.parent' => basename($filename));
        
        do {
            $parent = $values['.parent'];
            unset($values['.parent']);
            
            $values = array_merge(
                    parse_ini_file($dir.$parent, true),
                    $values);
        } while (isset($values['.parent']));
        
        return $values;
    }
    else {
        throw new RuntimeException("No such ini file: $filename");
    }
}
